<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <el-tree :data="title" :props="{ label: 'name' }">
          <template #default="obj">
            <treeTools :obj="obj" @showFormAdd="showFormAdd" />
          </template>
        </el-tree>
        <hr>
        <!-- 表单信息组件 -->
        <formInformation ref="formShow" @getDepartment="getDepartment" />
        <!-- 树形控件自动生成 -->
        <el-tree :data="depts" :props="{ label: 'name' }">
          <template #default="obj">
            <treeTools :obj="obj" @getDepartment="getDepartment" @showFormAdd="showFormAdd" @showFormEdit="showFormEdit" />
          </template>
        </el-tree>
      </el-card>
    </div>
  </div>
</template>

<script>
import treeTools from '@/components/departments/tree-tools.vue'
import formInformation from '@/components/departments/formInformation.vue'
import { inquireDepartment } from '@/api/departments'
import { listToTreeData } from '@/utils'
export default {
  components: { treeTools, formInformation },
  data() {
    return {
      title: [
        { name: '江苏传智播客教育科技股份有限公司', manager: '负责人', isOt: true, id: '', pid: '' }
      ],
      depts: [], // 部门列表
      dialogFormVisible: false // 表单显示与隐藏
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    // 请求部门
    async getDepartment() {
      const res = await inquireDepartment()
      this.depts = listToTreeData(res.depts, '')
      // console.log('加工', this.depts)
      // console.log('未加工', res)
    },
    // 显示添加表单
    showFormAdd(id, pid) {
      this.$refs.formShow.dialogFormVisible = true
      this.$refs.formShow.form.pid = id
      this.$refs.formShow.ppid = pid
    },
    // 显示编辑表单
    async showFormEdit(id) {
      await this.$refs.formShow.getDepartment(id)
      this.$refs.formShow.dialogFormVisible = true
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
